home *** CD-ROM | disk | FTP | other *** search
/ Aminet 21 / Aminet 21 (1997)(GTI - Schatztruhe)[!][Oct 1997].iso / Aminet / gfx / show / gs_src_gs.lha / gs5.03 / zbseq.c < prev    next >
C/C++ Source or Header  |  1997-04-21  |  8KB  |  270 lines

  1. /* Copyright (C) 1990, 1995, 1997 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* zbseq.c */
  20. /* Level 2 binary object sequence operators */
  21. #include "memory_.h"
  22. #include "ghost.h"
  23. #include "errors.h"
  24. #include "oper.h"
  25. #include "stream.h"
  26. #include "strimpl.h"
  27. #include "sfilter.h"
  28. #include "ialloc.h"            /* for isave.h */
  29. #include "idict.h"
  30. #include "iname.h"
  31. #include "isave.h"
  32. #include "ibnum.h"
  33. #include "btoken.h"
  34. #include "bseq.h"
  35. #include "store.h"
  36.  
  37. /* Current binary format (in iscan.c) */
  38. extern ref ref_binary_object_format;
  39.  
  40. /* System and user name arrays. */
  41. ref system_names, user_names;
  42. private ref *system_names_p = &system_names;
  43. private ref *user_names_p = &user_names;
  44. private gs_gc_root_t system_names_root, user_names_root;
  45.  
  46. /* Import the Level 2 scanner extensions. */
  47. typedef struct scanner_state_s scanner_state;
  48. extern int scan_binary_token(P3(stream *, ref *, scanner_state *));
  49. extern int (*scan_btoken_proc)(P3(stream *, ref *, scanner_state *));
  50.  
  51. /* Forward references */
  52. private void store_ushort(P3(byte *, ushort, int));
  53. private void store_long(P3(byte *, long, int));
  54.  
  55. /* Initialize the binary token machinery. */
  56. private void
  57. zbseq_init(void)
  58. {    /* Initialize fake system and user name tables. */
  59.     /* PostScript code will install the real system name table. */
  60.     make_empty_array(&system_names, a_readonly);
  61.     gs_register_ref_root(imemory, &system_names_root,
  62.                  (void **)&system_names_p, "system_names");
  63.     make_empty_array(&user_names, a_all);
  64.     gs_register_ref_root(imemory, &user_names_root,
  65.                  (void **)&user_names_p, "user_names");
  66.     /* Set up Level 2 scanning constants. */
  67.     scan_btoken_proc = scan_binary_token;
  68. }
  69.  
  70. /* <names> .installsystemnames - */
  71. private int
  72. zinstallsystemnames(register os_ptr op)
  73. {    if ( r_space(op) != avm_global )
  74.       return_error(e_invalidaccess);
  75.     check_read_type(*op, t_shortarray);
  76.     ref_assign_old(NULL, &system_names, op, ".installsystemnames");
  77.     pop(1);
  78.     return 0;
  79. }
  80.  
  81. /* - currentobjectformat <int> */
  82. private int
  83. zcurrentobjectformat(register os_ptr op)
  84. {    push(1);
  85.     *op = ref_binary_object_format;
  86.     return 0;
  87. }
  88.  
  89. /* <int> setobjectformat - */
  90. private int
  91. zsetobjectformat(register os_ptr op)
  92. {    check_type(*op, t_integer);
  93.     if ( op->value.intval < 0 || op->value.intval > 4 )
  94.         return_error(e_rangecheck);
  95.     ref_assign_old(NULL, &ref_binary_object_format, op, "setobjectformat");
  96.     pop(1);
  97.     return 0;
  98. }
  99.  
  100. /*
  101.  * The remaining operators in this file are conversion operators
  102.  * that do the dirty work of printobject and writeobject.
  103.  * The main control is in PostScript code, so that we don't have to worry
  104.  * about interrupts or callouts in the middle of writing the various data
  105.  * items.
  106.  */
  107.  
  108. /* <top_length> <total_length> <string8> .bosheader <string4|8> */
  109. private int
  110. zbosheader(register os_ptr op)
  111. {    int order = (int)ref_binary_object_format.value.intval - 1;
  112.     long top, total;
  113.     byte *p;
  114.  
  115.     if ( order < 0 )
  116.       return_error(e_undefined);
  117.     check_type(op[-2], t_integer);
  118.     check_type(op[-1], t_integer);
  119.     check_write_type(*op, t_string);
  120.     if ( r_size(op) < 8 )
  121.       return_error(e_rangecheck);
  122.     top = op[-2].value.intval;
  123.     total = op[-1].value.intval;
  124.     p = op->value.bytes;
  125.     if ( top <= 255 && total <= 0xffff - 4 )  /* use short format */
  126.       {    p[1] = (byte)top;
  127.         store_ushort(p + 2, (ushort)(total + 4), order);
  128.         r_set_size(op, 4);
  129.       }
  130.     else if ( top <= 0xffff ) /* use long format */
  131.       {    p[1] = 0;
  132.         store_ushort(p + 2, (ushort)top, order);
  133.         store_long(p + 4, total + 8, order);
  134.         r_set_size(op, 8);
  135.       }
  136.     else
  137.       return_error(e_rangecheck);
  138.     p[0] = bt_seq + order;
  139.     op[-2] = *op;
  140.     pop(2);
  141.     return 0;
  142. }
  143.  
  144. /* <ref_offset> <char_offset> <obj> <string8> .bosobject */
  145. /*   <ref_offset'> <char_offset'> <string8> */
  146. /* This converts a single object to its binary object sequence */
  147. /* representation.  Note that this may or may not modify the 'unused' field. */
  148. private int
  149. zbosobject(os_ptr op)
  150. {    register os_ptr op1 = op - 1;
  151.     int order = (int)ref_binary_object_format.value.intval - 1;
  152.     bin_seq_obj ob;
  153.     ref nstr;
  154.     check_type(op[-3], t_integer);
  155.     check_type(op[-2], t_integer);
  156.     check_write_type(*op, t_string);
  157.     if ( r_size(op) < 8 )
  158.         return_error(e_rangecheck);
  159. #define swap_t(a, b) t = a, a = b, b = t
  160. #if arch_is_big_endian
  161. #  define must_swap() (order & 1)
  162. #else
  163. #  define must_swap() (!(order & 1))
  164. #endif
  165.     switch ( r_type(op1) )
  166.     {
  167.     case t_null:
  168.         ob.tx = (byte)bs_null;
  169.         break;
  170.     case t_mark:
  171.         ob.tx = (byte)bs_mark;
  172.         break;
  173.     case t_integer:
  174.         ob.tx = (byte)bs_integer;
  175.         ob.value.w = op1->value.intval;
  176. num:        ob.size.w = 0;    /* (matters for reals) */
  177. swb:        /* swap bytes of value if needed */
  178.         if ( must_swap() )
  179.         { byte t;
  180.           swap_t(ob.value.b[0], ob.value.b[3]);
  181.           swap_t(ob.value.b[1], ob.value.b[2]);
  182.         }
  183.         break;
  184.     case t_real:
  185.         ob.tx = (byte)bs_real;
  186.         ob.value.f = op1->value.realval;
  187.         /***** handle non-IEEE native *****/
  188.         goto num;
  189.     case t_boolean:
  190.         ob.tx = (byte)bs_boolean;
  191.         ob.value.w = op1->value.boolval;
  192.         goto num;
  193.     case t_array:
  194.         ob.tx = (byte)bs_array;
  195.         if ( r_has_attr(op1, a_executable) )
  196.           ob.tx += (byte)bs_executable;
  197.         ob.size.w = r_size(op1);
  198.         ob.value.w = op[-3].value.intval;
  199.         op[-3].value.intval += ob.size.w * (ulong)sizeof(bin_seq_obj);
  200.         goto nsa;
  201.     case t_dictionary:        /* EXTENSION */
  202.         ob.tx = (byte)bs_dictionary;
  203.         if ( r_has_attr(op1, a_executable) )
  204.           ob.tx += (byte)bs_executable;
  205.         ob.size.w = dict_length(op1) << 1;
  206.         ob.value.w = op[-3].value.intval;
  207.         op[-3].value.intval += ob.size.w * (ulong)sizeof(bin_seq_obj);
  208.         goto nsa;
  209.     case t_string:
  210.         ob.tx = (byte)bs_string;
  211.         if ( r_has_attr(op1, a_executable) )
  212.           ob.tx += (byte)bs_executable;
  213.         ob.size.w = r_size(op1);
  214. nos:        ob.value.w = op[-2].value.intval;
  215.         op[-2].value.intval += ob.size.w;
  216. nsa:        if ( must_swap() )
  217.         { byte t;
  218.           swap_t(ob.size.b[0], ob.size.b[1]);
  219.         }
  220.         goto swb;
  221.     case t_name:
  222.         ob.tx = (byte)bs_name;
  223.         name_string_ref(op1, &nstr);
  224.         ob.size.w = r_size(&nstr);
  225.         goto nos;
  226.     }
  227.     memcpy(op->value.bytes, (byte *)&ob, sizeof(bin_seq_obj));
  228.     op[-1] = *op;
  229.     r_set_size(op - 1, 8);
  230.     pop(1);
  231.     return 0;
  232. }
  233.  
  234. /* ------ Initialization procedure ------ */
  235.  
  236. BEGIN_OP_DEFS(zbseq_l2_op_defs) {
  237.         op_def_begin_level2(),
  238.     {"1.installsystemnames", zinstallsystemnames},
  239.     {"0currentobjectformat", zcurrentobjectformat},
  240.     {"1setobjectformat", zsetobjectformat},
  241.     {"3.bosheader", zbosheader},
  242.     {"4.bosobject", zbosobject},
  243. END_OP_DEFS(zbseq_init) }
  244.  
  245. /* ------ Internal routines ------ */
  246.  
  247. /* Put a short (16 bits). */
  248. private void
  249. store_ushort(register byte *p, ushort num, int order)
  250. {    byte a = (byte)num;
  251.     byte b = (byte)(num >> 8);
  252.     if ( order & 1 )
  253.       p[0] = a, p[1] = b;
  254.     else
  255.       p[0] = b, p[1] = a;
  256. }
  257.  
  258. /* Put a long (32 bits). */
  259. private void
  260. store_long(register byte *p, long num, int order)
  261. {    byte a = num & 0xff;
  262.     byte b = (byte)(num >> 8);
  263.     byte c = (byte)(num >> 16);
  264.     byte d = (byte)(num >> 24);
  265.     if ( order & 1 )
  266.       p[0] = a, p[1] = b, p[2] = c, p[3] = d;
  267.     else
  268.       p[0] = d, p[1] = c, p[2] = b, p[3] = a;
  269. }
  270.